<?php
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2007-2008 Starbugs (univis2typo3@googlegroups.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  A copy is found in the textfile GPL.txt and important notices to the license
 *  from the author is found in LICENSE.txt distributed with these scripts.
 *
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
/**
 * Module: Web>UnivIS2Typo3
 *
 * Description:
 *
 * Notice:
 *
 *
 * @author      Starbugs <univis2typo3@googlegroups.com>
 */


class ProgressBar
{

    function __construct($bgColor, $isInteractive=true)
    {
	    $this->isInteractive = $isInteractive;
	    if ($this->isInteractive) {
		echo '
		      <div style="width:100%; height:20px; border: 1px solid black;">
		      <div id="progress-bar" style="float: left; width: 0%; height: 20px; background-color:green;">&nbsp;</div>
		      <div id="transparent-bar" style="float: left; width: 100%; height: 20px; background-color:'.$bgColor.';">&nbsp;</div>
		      </div>
		      <p id="progress-message">&nbsp;</p><br />';
	    }
    }

    function updateProgressBar()
    {
	    if ($this->isInteractive) {
		echo ('<script>
			document.getElementById("progress-bar").style.width = "'.$this->getCurrentProgress().'%";
			document.getElementById("transparent-bar").style.width = "'.(100-$this->getCurrentProgress()).'%";
			document.getElementById("progress-message").firstChild.data="'. $this->getUpdateInfo().'";
			</script>
		');
		flush();
		ob_flush();
	    } else {
		    print '['.number_format($this->getCurrentProgress(), 0, ',', '.').'%] '.$this->getUpdateInfo()."\n";
	    }
    }

    function setCurrentProgress($progress)
    {
	//FIXME actual progress is close to the currentProgress
	// due to rounding and numerical errors it might be bigger.
	// variation of 1 allowed as solution
	if ($progress >= ($this->currentProgress-1))
	{
	    if ($progress <= 101) { 
		$this->currentProgress = $progress;
	    } else {
		die("Error in ProgressBar: Progress > 100 !!!");
	    }
	}
    	else
	{
		die("Error in ProgressBar: setCurrentProgress < currentProgress !!!");

	}
	$this->updateProgressBar();
    }

    function getCurrentProgress()
    {
	return $this->currentProgress;
    }

    function getCurrentStepEndProgress()
    {
	return (($this->currentSteps+1)*$this->stepSize/$this->nSteps);
    }

    function setUpdateInfo($info)
    {
	$this->updateInfo = $info;
	$this->updateProgressBar();
    }

    function getUpdateInfo()
    {
	return $this->updateInfo;
    }

    function nextStep()
    {
	$this->currentSteps++;
	$this->setCurrentProgress($this->currentSteps*$this->stepSize/$this->nSteps);
	$this->updateProgressBar();
    }

    function setSteps($n)
    {
	$this->nSteps = $n;
    }

    private $currentProgress = 0;
    private $currentSteps = 0;
    private $nSteps = 1; 
    private $stepSize = 100;
    private $updateInfo = "ProgressBar Information";
    private $isInteractive = true;

}

?>
